home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / fcompare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  1.7 KB  |  71 lines

  1. /*
  2. **  FCOMPARE.C - Compare 2 files
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. enum {ERROR = -1, SUCCESS, FAIL};
  12.  
  13. #define BUFSIZE 16384
  14. static char buf[2][BUFSIZE];
  15.  
  16. int fcompare(const char *fname1, const char *fname2)
  17. {
  18.       FILE *f1, *f2;
  19.       long len1, len2;
  20.       int retval = SUCCESS;
  21.  
  22.       if (NULL == (f1 = fopen(fname1, "rb")))
  23.             return ERROR;
  24.       if (NULL != (f2 = fopen(fname2, "rb")))
  25.       {
  26.             size_t size1, size2;
  27.  
  28.             fseek(f1, 0L, SEEK_END);
  29.             fseek(f2, 0L, SEEK_END);
  30.  
  31.             if (ftell(f1) != ftell(f2))
  32.                   retval = FAIL;
  33.             else
  34.             {
  35.                   rewind(f1);
  36.                   rewind(f2);
  37.                   do
  38.                   {
  39.                         size1 = fread(buf[0], 1, BUFSIZE, f1);
  40.                         size2 = fread(buf[1], 1, BUFSIZE, f2);
  41.                         if (0 == (size1 | size2))
  42.                               break;
  43.                         if ((size1 != size2) || memcmp(buf[0], buf[1], size1))
  44.                         {
  45.                               retval = FAIL;
  46.                               break;
  47.                         }
  48.                   } while (size1 && size2);
  49.             }
  50.             fclose(f2);
  51.       }
  52.       else  retval = ERROR;
  53.       fclose(f1);
  54.       return retval;
  55. }
  56.  
  57. #ifdef TEST
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.       if (3 > argc)
  62.       {
  63.             puts("Usage: FCOMPARE file1 file2");
  64.             return 1;
  65.       }
  66.       printf("fcompare(%s, %s) returned %d\n", argv[1], argv[2],
  67.             fcompare(argv[1], argv[2]));
  68. }
  69.  
  70. #endif /* TEST */
  71.